package cn.wizzer.app.web.modules.controllers.platform.sys;


import cn.wizzer.app.sys.modules.models.SysCsAccount;
import cn.wizzer.app.sys.modules.services.SysCsAccountServices;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;

/**
 * 客服配置账号
 */
@IocBean
@At("/platform/sys/cs/account")
public class SysCsAccountController {

    @Inject
    @Reference
    private SysCsAccountServices sysCsAccountServices;

    @At("")
    @Ok("beetl:/platform/sys/csAccount/index.html")
    @RequiresPermissions("sys.manager.cs.account")
    public void index(HttpServletRequest req) {
    }


    /**
     * 客服配置账号列表
     *
     * @param searchKeyword 查询值
     * @param searchDate    时间
     * @param pageNumber    当前页
     * @param pageSize      当前页显示条数
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.cs.account")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("type") String type,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        Cnd cnd = Cnd.NEW();
        // 平台链接地址
        if (Strings.isNotBlank(searchKeyword)) {
            cnd.and("uri", "like", "%" + searchKeyword + "%");
        }
        // 平台类型(LINE,TELGRAM,WHATSAPP)
        if (Strings.isNotBlank(type)) {
            cnd.and("type", "=", type);
        }
        // 时间
        if (searchDate != null && searchDate.length > 0) {
            cnd.and("opAt", ">=", Times.d2TS(Times.D(searchDate[0])))
                    .and("opAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        cnd.orderBy("opAt", "desc");
        return Result.success().addData(sysCsAccountServices.listPage(pageNumber, pageSize, cnd));
    }


    /**
     * 添加客服配置账号
     *
     * @param
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.cs.account.add")
    public Object add(@Param("..") SysCsAccount sysCsAccount) {
        // 其他相同类型的状态修改
        if (sysCsAccount.getStatus()) {
            sysCsAccountServices.update(Chain.make("status", "0"), Cnd.where("type", "=", sysCsAccount.getType()));
        }
        sysCsAccountServices.insert(sysCsAccount);
        return Result.success();
    }


    /**
     * 更新客服配置账号（包括启用禁用）
     *
     * @param sysCsAccount
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.cs.account.edit")
    public Object edit(@Param("..") SysCsAccount sysCsAccount) {
        // 其他相同类型的状态修改
        if (sysCsAccount.getStatus()) {
            sysCsAccountServices.update(Chain.make("status", "0"), Cnd.where("type", "=", sysCsAccount.getType()));
        }
        sysCsAccountServices.updateIgnoreNull(sysCsAccount);
        return Result.success();
    }


    /**
     * 删除客服配置账号
     *
     * @param id
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.cs.account.delete")
    public Object delete(@Param("id") String id) {
        sysCsAccountServices.delete(id);
        return Result.success();
    }

}
